﻿using System;
using System.Data;
using System.Windows.Forms;

namespace DevTeam
{
    public partial class UserChanger : DevPopupForm
    {
        public UserChanger()
        {
            InitializeComponent();
            /*
            
            //riCikisTipi.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            
            */

            //RepositoryItemGridLookUpEdit riCikisTipi = GetCustomGLE(RepositoryManager.dsLookupsV_User, "Username", "UserId");
            //gleUser.Properties.Assign(riCikisTipi);

            gleUser.Properties.Assign(RepositoryManager.Get(Session.UserRepositoryId));
            gleUser.EditValue = WhoAmI;
        }

        private void UserChanger_Load(object sender, EventArgs e)
        {
            gleUser.Closed += gleUser_Closed;
        }

        void gleUser_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            int userId = (int)gleUser.EditValue;
            DataRow newUser = RepositoryManager.dsLookups.Tables["V_User"].Select("UserId=" + userId)[0];

            Session.CurrentUser.UserId = newUser["UserId"].ToInt();
            Session.CurrentUser.DepartmentId = newUser["DepartmentId"].ToInt();
            Session.CurrentUser.Email = newUser["Email"].ToStr();
            Session.CurrentUser.Name = newUser["Name"].ToStr();
            Session.CurrentUser.UserName = newUser["Username"].ToStr();
            Session.CurrentUser.UserId = userId;
            Session.CurrentUser.RefreshYetki();
            Session.MainForm.bsiUser.Caption = Session.CurrentUser.UserName + " / " + Session.DatabaseTipi;
            //Notify("Yeni Kullanıcı: " + newUser.Name);            
            this.DialogResult = DialogResult.OK;
        }
    }
}
